package com.suse.service.admin.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.service.admin.edu.mapper.VideoMapper;
import com.suse.service.admin.edu.model.entity.Video;
import com.suse.service.admin.edu.model.param.VideoSaveParam;
import com.suse.service.admin.edu.model.param.VideoUpdateParam;
import com.suse.service.admin.edu.model.vo.VideoInfoVo;
import com.suse.service.admin.edu.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.suse.common.config.response.SimpleCode.*;


/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-11
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Override
    public R getVideoInfoById(String id) {
        Video video = lambdaQuery()
                .select(Video::getId,
                        Video::getTitle,
                        Video::getIsFree,
                        Video::getSort)
                .eq(Video::getId, id)
                .one();
        VideoInfoVo videoInfoVo = BeanUtil.copyProperties(video, VideoInfoVo.class);
        return R.success(videoInfoVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveVideo(VideoSaveParam videoSaveParam) {
        Video video = BeanUtil.copyProperties(videoSaveParam, Video.class);
        if (save(video)) {
            return R.success(SAVE_SUCCESS);
        }else {
            throw new ApiException(SAVE_ERROR);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateVideo(VideoUpdateParam videoUpdateParam) {
        Video video = BeanUtil.copyProperties(videoUpdateParam, Video.class);
        if (updateById(video)) {
            return R.success(UPDATE_SUCCESS);
        }else {
            throw new ApiException(UPDATE_ERROR);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R removeVideoById(String id) {
        // TODO 删除云视频

        if (removeById(id)) {
            return R.success(REMOVE_SUCCESS);
        }else {
            throw new ApiException(REMOVE_ERROR);
        }
    }

}
